package cn.laixueit;

import cn.laixueit.jwt.JwtUtils;
import cn.laixueit.jwt.RsaUtils;
import cn.laixueit.jwt.UserInfo;
import org.junit.Before;
import org.junit.Test;

import java.security.PrivateKey;
import java.security.PublicKey;

public class Demo {

    private static final String pubKeyPath = "d:\\rsa.pub";

    private static final String priKeyPath = "d:\\rsa.pri";

    private PublicKey publicKey;

    private PrivateKey privateKey;

    @Before //生成之前需要注释
    public void Demo() throws Exception {
        this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
    }

    @Test
    public void genterateToken() throws Exception {
        UserInfo admin = new UserInfo(1l, "admin");
        String token = JwtUtils.generateToken(admin, privateKey, 5);
        System.out.println("token = " + token);
    }

    @Test
    public void t() throws Exception {
        RsaUtils.generateKey(pubKeyPath,priKeyPath,"123");
    }

    @Test
    public void testParseToken() throws Exception {
        String token = "eyJhbGciOiJSUzI1NiJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsImV4cCI6MTY1MDI1MzY3MH0.Qp1EOpgE4QMlj1duVvkxnadv60cV6b-Id2wKZA2YqBGRSwZp5aeMDOpKzuQrO-FeutlPmAvfMRQbnLmYaKy6eVy5Rj9Vkqzs_hhogzx5nCCQbwbJRoBnr_VeHIBs_xM-pcrWPfYZA7Uj5jcPWc322HCdvn75V9GVfUxJfVK9uwg";
        // 解析token
        UserInfo user = JwtUtils.getInfoFromToken(token, publicKey);
        System.out.println("id: " + user.getId());
        System.out.println("userName: " + user.getUsername());
    }
}
